package com.me.study.ui.activity

import android.os.Bundle
import android.os.Handler
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ktx.immersionBar
import com.me.study.R
import com.me.study.app.base.BaseActivity
import com.me.study.app.ext.goActivity
import com.me.study.app.general.LaunchView
import com.me.study.databinding.ActivityInitiationBinding
import com.me.study.viewmodel.SampleViewModel

class InitiationActivity : BaseActivity<SampleViewModel, ActivityInitiationBinding>(),
    LaunchView.OnAnimationListener {

    private var isStart = false

    override fun initImmersionBar() {
        immersionBar {
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.transparent)
            statusBarDarkFont(true)
            fullScreen(true)
            navigationBarDarkIcon(true)
            hideBar(BarHide.FLAG_HIDE_BAR)
        }
    }

    override fun layoutId() = R.layout.activity_initiation

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.launchView.setOnAnimationListener(this)
        mDatabind.launchView.startAnimator()
    }

    override fun onResume() {
        super.onResume()
        Handler(mainLooper).postDelayed({
            startMainActivity(200)
        }, 2000)
    }

    override fun onBackPressed() {
        startMainActivity(200)
    }

    private fun finishThis() {
        this@InitiationActivity.finish()
    }

    override fun onAnimationEnd(isEnter: Boolean) {
        if (!isEnter) {
            finishThis()
        }
    }

    private fun startMainActivity(timer: Long) {
        if (isStart) {
            return
        }
        isStart = true
        Handler(mainLooper).postDelayed({
            goActivity(MainActivity::class.java)
            overridePendingTransition(
                R.anim.transparent_activity_enter,
                R.anim.transparent_activity_exit
            )
        }, timer)
        mDatabind.launchView.endAnimator()
    }
}